#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

class student
{
public:
    string name;
    int mt;
    int en;
    int ph;
    int total;
    int avg;

    void calu()
    {
        this->total=mt+en+ph;
        this->avg=this->total/3;
    }

    void output()
    {
        cout<<name<<"   ";
        cout<<"mt:"<<mt<<"   ";
        cout<<"en:"<<en<<"   ";
        cout<<"ph:"<<ph<<"   ";
        cout<<"total:"<<total<<"   ";
        cout<<"avg:"<<avg<<endl;
    }
};

void quicksort(int left,int right,student* allStudent[])
{
    if(left>right) //加减过度了说明全部处理完毕，递归结束
       return;

    int i,j;
    student* base=allStudent[left]; //基准数，这里选最左的
    //左右游标从两端出发
    i=left;
    j=right;

    while(i!=j) //左右游标相遇时本次排序结束
    {
        //基准数选最左，所以右边游标先移动。二者分别在左右找到一个失序的数
        while(allStudent[j]->total<=base->total && i<j)
            j--;
        while(allStudent[i]->total>=base->total && i<j)
            i++;

        if(i<j) //交换左右游标找到的两个失序的数
        {
            student* t=allStudent[i];
            allStudent[i]=allStudent[j];
            allStudent[j]=t;
        }
    }

    //此时基准数应该在本次left和right的中心，所以交换基准数和中心
    allStudent[left]=allStudent[i];
    allStudent[i]=base;

    quicksort(left,i-1,allStudent); //处理左边
    quicksort(i+1,right,allStudent); //处理右边
}

student* creatStudent()
{
    student* s=new student;
    cin>>s->name;
    cin>>s->mt;
    cin>>s->en;
    cin>>s->ph;
    s->calu();
    return s;
}

int size;

void outputAllStudent(student* allStudent[])
{
    for(int i=0;i<size;i++)
        allStudent[i]->output();
}

student* findStudent(string name,student* allStudent[])
{
    for(int i=0;i<size;i++)
    {
        if(allStudent[i]->name==name)
            return allStudent[i];
    }
}

int main()
{
    student* allStudent[30];
    printf("set student num:");
    scanf("%d",&size);
    for(int i=0;i<size;i++)
    {
        printf("plase input %d student:",i+1);
        allStudent[i]=creatStudent();
    }

    quicksort(0,size-1,allStudent);
    outputAllStudent(allStudent);

    while(1)
    {
        printf("Find:");
        string input;
        cin>>input;
        findStudent(input,allStudent)->output();
    }
}
